//
//  BaseNavigationController.m
//  Connections
//
//  Created by beisheng03 on 2017/9/28.
//  Copyright © 2017年 beisheng. All rights reserved.
////

//
//// 获取当前类下所有的tabBarItem
//UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
//
//// 设置选中标题的颜色
//// 创建富文本属性字典
//NSMutableDictionary *testAttr = [NSMutableDictionary dictionary];
//
//// 描述颜色
//testAttr[NSForegroundColorAttributeName] = colorWith(47, 162, 19);
//
//// 设置标题的富文本属性
//[item setTitleTextAttributes:testAttr forState:UIControlStateSelected];
//
//// 问题:设置字体颜色不好使,注意:一定要先设置正常状态下字体大小
//// 设置正常状态下字体
//NSMutableDictionary *testAttrNor = [NSMutableDictionary dictionary];
//
//// 描述字体
//testAttrNor[NSFontAttributeName] = MYFONT(13);
//
//// 设置标题的富文本属性
//[item setTitleTextAttributes:testAttrNor forState:UIControlStateNormal];

#import "BaseNavigationController.h"
#import "WineCategoryVC.h"
#import "PaySuccessJumpVC.h"
#import "PayFaildJumpVC.h"
@interface BaseNavigationController ()<UINavigationBarDelegate,UIGestureRecognizerDelegate>

@end

@implementation BaseNavigationController


+ (void)initialize
{
    if (self == [BaseNavigationController class]) {
        
        UINavigationBar *gationBar = [UINavigationBar appearanceWhenContainedIn:self, nil];
        
        UIBarButtonItem *barItem = [UIBarButtonItem appearanceWhenContainedIn:self, nil];
    
        NSMutableDictionary *testAttrNor = [NSMutableDictionary dictionary];
        
        // 描述字体
        testAttrNor[NSFontAttributeName] = MYFONT(15);
        
        // 设置标题的富文本属性
        [barItem setTitleTextAttributes:testAttrNor forState:UIControlStateNormal];
        
        gationBar.tintColor = [UIColor whiteColor];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        dic[NSFontAttributeName] = MYFONT(17);
        
        dic[NSForegroundColorAttributeName] = [UIColor whiteColor];
        
        [gationBar setTitleTextAttributes:dic];
        
        
        
        //        [gationBar setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRed:38.0/255.0 green:133.0/255.0  blue:216.0/255.0 alpha:0.9]] forBarMetrics:UIBarMetricsDefault];
        
        [gationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];

        [gationBar setBackgroundImage:[UIImage imageWithMyColor:[UIColor colorWithRed:0.0/255.0 green:153.0/255.0  blue:255.0/255.0 alpha:0.98]] forBarMetrics:UIBarMetricsDefault];
        
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 默认一个导航控制器都是有滑动返回功能
    // 只要把系统的返回按钮覆盖,滑动返回功能实现
    // 滑动返回功能 -> 1.手势被干掉 pass 2.代理 -> 根控制器不需要滑动返回
    // 触发手势,就会调用Target的action
    id target = self.interactivePopGestureRecognizer.delegate;
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:target action:@selector(handleNavigationTransition:)];
    pan.delegate = self;
    [self.view addGestureRecognizer:pan];
    
    self.interactivePopGestureRecognizer.enabled = NO;
    // Do any HomeViewControlleradditional setup after loading the view.
}

- (void)handleNavigationTransition:(UIGestureRecognizer *)giseture{}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    // 只有非根控制器,才需要设置返回按钮
    // 0
    if (self.childViewControllers.count > 0 ) { // 非根控制器
        
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIButton *but = [UIButton buttonWithType:UIButtonTypeCustom];
        [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        [but setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateHighlighted];
        
        [but addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];

        
        but.imageView.contentMode = UIViewContentModeScaleAspectFit;
        
//        but.backgroundColor = [UIColor redColor];
        
        but.frame = CGRectMake(0, 0, 44, 44);
        but.contentEdgeInsets = UIEdgeInsetsMake(11, -10, 11, 22);
        
        UIBarButtonItem *returnButtonItem = [[UIBarButtonItem alloc]initWithCustomView:but];
        
        viewController.navigationItem.leftBarButtonItem = returnButtonItem;
        
        
    }
    
//    viewController.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    
    [super pushViewController:viewController animated:animated];
}


- (void)back{
    
    
    [self popViewControllerAnimated:YES];
}

//#import ".h"
//#import ".h"
#pragma mark - UIGestureRecognizerDelegate
// 是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    
    if ([self.childViewControllers.lastObject isKindOfClass:[WineCategoryVC class]] || [self.childViewControllers.lastObject isKindOfClass:[PaySuccessJumpVC class]]  || [self.childViewControllers.lastObject isKindOfClass:[PayFaildJumpVC class]] ) {
        
        return NO;
        
    } else {
        return self.childViewControllers.count > 1;

    }
    
}



- (UIStatusBarStyle)preferredStatusBarStyle{
    
    
    return UIStatusBarStyleLightContent;
    
}

@end
